package com.live.shop.scene.server.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.live.shop.R;
import com.live.shop.model.ProductGroupModel;

import java.util.List;

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.AdapterHolder> {
    private final Context mContext;
    //素材列表
    private final List<ProductGroupModel> list;
    //选中的post
    private int selectPosition = -1;
    //素材选选择监听器
    private OnMaterialClickListener materialListener;

    public GroupAdapter(Context context,List<ProductGroupModel> mlist) {
        this.mContext = context;
        this.list = mlist;
    }

    @NonNull
    @Override
    public AdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AdapterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ws_main_group, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterHolder holder, @SuppressLint("RecyclerView") int position) {
        //根据当前类似是否为视频进行显示
        ProductGroupModel model = list.get(position);
        holder.item_ws_main_group_name.setText(model.getProductGroupName());
        //是否选中
        if(selectPosition == position){
            holder.item_ws_main_group_name.setTextColor(mContext.getColor(R.color.white));
            holder.item_ws_main_group_name.setBackgroundResource(R.drawable.bg_stroke_5dip_white);
        }else{
            holder.item_ws_main_group_name.setTextColor(mContext.getColor(R.color.gray));
            holder.item_ws_main_group_name.setBackgroundResource(R.drawable.bg_stroke_5dip_gray);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置选中的位置
                selectPosition = position;
                if(materialListener != null){
                    materialListener.onMaterialClick(position);
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    static class AdapterHolder extends RecyclerView.ViewHolder {
        TextView item_ws_main_group_name;
        public AdapterHolder( View itemView) {
            super(itemView);
            item_ws_main_group_name = itemView.findViewById(R.id.item_ws_main_group_name);
        }
    }
    //切换产品组
    public void setGroup(int position){
        selectPosition = position;
        notifyDataSetChanged();
    }
    //素材点击
    public void setOnMaterialClickListener(OnMaterialClickListener onMaterialListener) {
        materialListener = onMaterialListener;
    }
}
